package com.kzd.gtgame.main.home

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.entity.GameLottery
import com.kzd.gtgame.jetpack.vm.HomeViewModel
import com.kzd.gtgame.main.KEY_HOME_LOTTERY
import com.kzd.gtgame.main.checkLogin
import com.kzd.gtgame.main.popup.LotteryConPopup
import com.kzd.gtgame.main.popup.LotteryObtPopup
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_home_lottery.*
import java.lang.StringBuilder

class HomeLotteryActivity : OwnerViewModeActivity<HomeViewModel>() {

    private var mGameLottery: GameLottery? = null

    private val mLotteryConPopup: LotteryConPopup by lazy {
        LotteryConPopup(context = this)
    }

    private val mLotteryObtPopup: LotteryObtPopup by lazy {
        LotteryObtPopup(context = this)
    }

    override val layoutResId: Int
        get() = R.layout.activity_home_lottery

    override fun viewModel(): HomeViewModel {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = false, view = llHomeLotteryBar)
    }

    @SuppressLint("ResourceType")
    override fun initView() {
        ivHomeLotteryBack.setOnClickListener {
            onBackPressed()
        }

        //抽奖1次
        ivHomeLotteryOneNum.setOnClickListener {
            checkLogin {
                viewModel.homeLotteryObtain(type = 0)
            }
        }

        //抽奖10次
        ivHomeLotteryTenNum.setOnClickListener {
            checkLogin {
                viewModel.homeLotteryObtain(type = 1)
            }
        }

        //中奖记录
        tvHomeLotteryRecord.setOnClickListener {
            checkLogin {
                viewModel.homeLotteryRecord()
            }
        }

        //获取抽奖次数
        ivHomeLotteryGetNum.setOnClickListener {
            val content = mGameLottery?.config
                ?: return@setOnClickListener
            mLotteryConPopup.setContent(content)
                .setBackgroundImage(R.mipmap.ic_90028)
                .showPopupWindow()
        }

        //抽奖明细
        ivHomeLotteryDetails.setOnClickListener {
            val content = mGameLottery?.mingxi
                ?: return@setOnClickListener
            mLotteryConPopup.setContent(content)
                .setBackgroundImage(R.mipmap.ic_90025)
                .showPopupWindow()
        }

        //抽奖说明
        ivHomeLotteryCaption.setOnClickListener {
            val content = mGameLottery?.shuoming
                ?: return@setOnClickListener
            mLotteryConPopup.setContent(content)
                .setBackgroundImage(R.mipmap.ic_90026)
                .showPopupWindow()
        }

        vfHomeLotteryMarquee.setFadingEdgeLength(500)
        vfHomeLotteryMarquee.setInAnimation(this, R.anim.anim_marquee_in)
        vfHomeLotteryMarquee.setOutAnimation(this, R.anim.anim_marquee_out)
    }

    override fun initData() {

        mGameLottery = intent?.extras?.getSerializable(KEY_HOME_LOTTERY)
                as? GameLottery
        mGameLottery?.let { setMarquee(it) }

        viewModel.homeLottery()
    }

    private fun setMarquee(lottery: GameLottery) {
        lottery.awards_list.forEach {
            val textView = TextView(this)
            textView.layoutParams = LinearLayout.LayoutParams(-1, -1).apply {
                gravity = Gravity.CENTER
                marginStart = ConvertUtils.dp2px(10f)
                marginEnd = ConvertUtils.dp2px(10f)
            }
            val content = "${it.user_name}\t获得${it.awards_name}"
            textView.text = content
            textView.gravity = Gravity.CENTER
            textView.setTextColor(Color.WHITE)
            textView.textSize = 11f
            textView.setSingleLine()
            vfHomeLotteryMarquee.addView(textView)
        }
        if (lottery.awards_list.size > 1) {
            vfHomeLotteryMarquee.startFlipping()
        }
        tvHomeLotteryCount.text = StringBuilder().append("剩余抽奖次数：")
            .append(lottery.nums)
    }

    override fun onDestroy() {
        super.onDestroy()
        vfHomeLotteryMarquee.stopFlipping()
    }

    override fun initViewMode() {
        viewModel.homeLotteryLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            tvHomeLotteryCount.text = StringBuilder().append("剩余抽奖次数：")
                .append(data.nums)
            if (mGameLottery != null) {
                return@observe
            }
            mGameLottery = data
            mGameLottery?.let { setMarquee(it) }
        })

        viewModel.homeLotteryRecordLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mLotteryConPopup.setContentRecord(data)
                .setBackgroundImage(R.mipmap.ic_90027)
                .showPopupWindow()
        })

        viewModel.homeLotteryObtainLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mLotteryObtPopup.setContent(data)
                .showPopupWindow()
            viewModel.homeLottery()
        })
    }
}